#include "configurationview.h"
#include <QFormLayout>
#include <QLabel>
#include <QHeaderView>
#include <iostream>
#include "logger.h"

using namespace std;
using namespace views;
using namespace utilities;

ConfigurationView::ConfigurationView()
{
    initUI();
    setupUI();

    // Setup necessary event listeners
    connect(_btnStartVision, SIGNAL(clicked()), this, SLOT(emitStartVision()));
    connect(_btnStopVision, SIGNAL(clicked()), this, SLOT(emitStopVision()));
    connect(_btnAddInfo, SIGNAL(clicked()), this, SLOT(addInfoLine()));

    // Init
    _infoLines = new QVector<InformationLine*>();
}

ConfigurationView::~ConfigurationView()
{
    delete(_mainFBox);
    delete(_panelInfo);
    delete(_btnStartVision);
    delete(_btnStopVision);
    delete(_btnAddInfo);
    delete(_infoLines);
}

void ConfigurationView::initUI()
{
    Logger::logMessage("ConfigurationView::initUI()");

    // Initialize UI objects
    _mainFBox           = new QFrame();
    _btnStartVision     = new QPushButton(QString::fromUtf8("Démarrer vision"));
    _btnStopVision      = new QPushButton(QString::fromUtf8("Arrêter vision"));
    _btnAddInfo         = new QPushButton(QString::fromUtf8(" Ajouter un lien"));
    _btnSaveConfig      = new QPushButton(QString::fromUtf8("Enregistrer"));

    // Initialize attributes
    this->setMinimumSize(200, 300);

    // Btns start/stop vision
    _btnStartVision->setMaximumWidth(150);
    _btnStopVision->setMaximumWidth(150);

    // Btn add info
    _btnAddInfo->setMaximumWidth(150);
    _btnAddInfo->setIcon(QIcon("../Recyk/Data/img/addInfo.png"));

    // Btn save config
    _btnSaveConfig->setMinimumWidth(200);
    _btnSaveConfig->setMinimumHeight(40);
    _btnSaveConfig->setIcon(QIcon("../Recyk/Data/img/saveConf.png"));

    // Table info
    _tableInfo = new QTableWidget(0, 1);
    QStringList labels;
    labels << QString::fromUtf8("Documents à diffuser");
    _tableInfo->setHorizontalHeaderLabels(labels);
    _tableInfo->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
    _tableInfo->setShowGrid(false);
}

void ConfigurationView::setupUI()
{
    Logger::logMessage("ConfigurationView::setupUI()");

    // Setup the main layout
    QFormLayout *fLayout = new QFormLayout;

    // Title
    QGridLayout *layout = new QGridLayout;
    QLabel *title0   = new QLabel(QString::fromUtf8("Configuration"));
    title0->setStyleSheet(QString::fromUtf8("font: 12pt \"Rockwell\"; font-weight: bold; color: rgb(0, 0, 0);"));
    layout->addWidget(title0, 0, 0, 1, 1, Qt::AlignLeft | Qt::AlignTop);
    fLayout->addItem(layout);

    // Vision
    QLabel *vision = new QLabel(QString::fromUtf8("Vision"));
    vision->setStyleSheet(QString::fromUtf8("font: 8pt \"Rockwell\"; font-weight: bold; color: yellow; background-color: blue;"));
    fLayout->addWidget(vision);
    fLayout->addWidget(_btnStartVision);
    fLayout->addWidget(_btnStopVision);

    // Spacer
    fLayout->addWidget(new QLabel);

    // Information
    QLabel *info   = new QLabel(QString::fromUtf8("Informations à diffuser"));
    info->setStyleSheet(QString::fromUtf8("font: 8pt \"Rockwell\"; font-weight: bold; color: yellow; background-color: blue;"));
    fLayout->addWidget(info);
    fLayout->addWidget(_btnAddInfo);

    // Lay out the information table
    fLayout->addWidget(_tableInfo);

    // Lay out save button
    QHBoxLayout *layout2 = new QHBoxLayout;
    layout2->addWidget(_btnSaveConfig, 0, Qt::AlignCenter);
    fLayout->addItem(layout2);

    // Set the main layout
    this->setLayout(fLayout);
}

void ConfigurationView::setViewName(const QString& name)
{
    Logger::logMessage("ConfigurationView::setViewName(const QString& name)");
    _viewName = name;
}

void ConfigurationView::saveConfiguration()
{
    Logger::logMessage("ConfigurationView::saveConfiguration()");
}

void ConfigurationView::emitStartVision()
{
    Logger::logMessage("ConfigurationView::emitStartVision()");
    emit startVision();
}

void ConfigurationView::emitStopVision()
{
    Logger::logMessage("ConfigurationView::emitStopVision()");
    emit stopVision();
}

void ConfigurationView::addInfoLine()
{
    Logger::logMessage("ConfigurationView::addInfoLine()");

    int id = _infoLines->size();
    InformationLine *infoLine = new InformationLine(id, (LinkType)0, "");
    connect(infoLine, SIGNAL(autoDeletion(int)), this, SLOT(deleteInfoLine(int)));

    _infoLines->append(infoLine);
    _tableInfo->insertRow(_tableInfo->rowCount());
    _tableInfo->setCellWidget(id, 0, _infoLines->at(id));

    // Resize table to fit contents
    _tableInfo->resizeRowsToContents();
    _tableInfo->resizeColumnsToContents();
}

void ConfigurationView::adjustInformationLineIds()
{
    int i;
    int size = _infoLines->size();

    for (i = 0; i < size; ++i) {
        _infoLines->at(i)->setId(i);
    }
}

void ConfigurationView::deleteInfoLine(int id)
{
    Logger::logMessage("ConfigurationView::deleteInfoLine(int id)");

    _infoLines->remove(id);
    _tableInfo->removeRow(id);
    adjustInformationLineIds();
}
